package com.yanwq.sayhi.controller.club.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yanwq.sayhi.R;
import com.yanwq.sayhi.base.BaseActivity;
import com.yanwq.sayhi.base.BaseToolbarActivity;
import com.yanwq.sayhi.common.client.OkJsonObjectResponseCallback;
import com.yanwq.sayhi.common.client.OkRestClient;
import com.yanwq.sayhi.common.event.ClubEvent;
import com.yanwq.sayhi.dao.bean.Club;
import com.yanwq.sayhi.widget.MToast;

import butterknife.BindView;
import de.greenrobot.event.EventBus;

public class ClubMottoActivity extends BaseToolbarActivity {
    @BindView(R.id.club_motto)
    EditText mETMotto;

    private Club mClub;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_club_motto);
    }

    @Override
    public void init() {
        mClub = (Club) getIntent().getSerializableExtra("club");
    }

    @Override
    public void initView() {
        mToolbar.setTitle("修改俱乐部签名");
        mETMotto.setHint(mClub.getMotto());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_send, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        send();
        return true;
    }

    private void send() {
        if (TextUtils.isEmpty(mETMotto.getText())) {
            return;
        }

        updateMotto();
    }

    private void updateMotto() {
        mClub.setMotto(mETMotto.getText().toString());
        OkRestClient.getInstance().post(this, "/club/update", mClub, new OkJsonObjectResponseCallback() {
            @Override
            public void onStart() {
                showProgressDialog("正在修改");
            }

            @Override
            public void onSuccess(JSONObject jsonObject) {
                MToast.showShort(getApplicationContext(), "修改介绍成功");
                Intent intent = new Intent();
                intent.putExtra("club", JSON.toJavaObject(jsonObject, Club.class));
                setResult(0xf1, intent);
                delayFinish();
                EventBus.getDefault().post(new ClubEvent(ClubEvent.CHANGED));
            }

            @Override
            public void onStop() {
                cancelProgressDialog();
            }
        });
    }
}
